package com.framework.disconf.config;

import com.framework.disconf.property.ApplicationProperty;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * Mybatis Spring 配置类
 *
 * @author chenx
 * @date 2016/4/19
 */
@Configuration
public class MybatisSpringConfig {

    @Bean(name = "dbConfig")
    public ApplicationProperty dbConfig(){
        return new ApplicationProperty();
    }

    @Bean(name = "dataSource")
    @Primary
    public DataSource dataSource(ApplicationProperty applicationProperty){
        DataSource ds =  DataSourceBuilder.create()
                .url(applicationProperty.getUrl())
                .username(applicationProperty.getUsername())
                .password(applicationProperty.getPassword())
                .driverClassName(applicationProperty.getDriverClassName())
                .build();
        return ds;
    }

    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactoryBean sqlSessionFactory(@Qualifier("dataSource")DataSource dataSource) throws IOException {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
//        PageHelper pageHelperPlugin = new PageHelper();
//        Properties properties = new Properties();
//        properties.setProperty("dialect", "mysql");
//        pageHelperPlugin.setProperties(properties);
//
//        Interceptor[] plugins = new Interceptor[] {pageHelperPlugin};
//        factoryBean.setPlugins(plugins);

        return factoryBean;
    }

    @Bean(name = "mapperScannerConfigurer")
    @Primary
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.framework.disconf.dao.master");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }

    @Bean(name = "slaveDataSource")
    public DataSource slaveDataSource(ApplicationProperty applicationProperty){
        DataSource ds =  DataSourceBuilder.create()
                .url(applicationProperty.getSlaveUrl())
                .username(applicationProperty.getSlaveUsername())
                .password(applicationProperty.getSlavePassword())
                .driverClassName(applicationProperty.getSlaveDriverClassName())
                .build();
        return ds;
    }

    @Bean(name = "slaveSqlSessionFactory")
    public SqlSessionFactoryBean slaveSqlSessionFactory(@Qualifier("slaveDataSource")DataSource dataSource) throws IOException {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:slave/*.xml"));
        return factoryBean;
    }

    @Bean(name = "slaveMapperScannerConfigurer")
    public MapperScannerConfigurer slaveMapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.framework.disconf.dao.slave");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("slaveSqlSessionFactory");
        return mapperScannerConfigurer;
    }
}
